/*
 * @Author: zhengzhuang
 * @Date: 2020-07-22 15:05:27
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-01-28 17:39:34
 * @Description: 车主信息(司机信息)
 * @FilePath: /C20Driver/lib/provider/driver_information.dart
 */
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:C20Driver/model/driver_information_model.dart';
import 'package:C20Driver/model/vehicle_information_model.dart';
import 'package:C20Driver/service/request.dart';

class DriverInformation with ChangeNotifier {
  String _token;
  String get token => _token;
  DateTime now = new DateTime.now();

  ///是否需要刷脸 0000 需要
  String _needFace;

  ///是否重新刷脸 1需要，0不需要
  String _needNewFace = '0';

  ///司机个人信息
  var _driverJson;

  var _tabIndex = 0;

  ///刷脸阈值
  String _threshold;

  String get needFace => _needFace;
  String get needNewFace => _needNewFace;
  String get threshold => _threshold;
  int get tabIndex => _tabIndex;
  get driverJson => _driverJson;

  Future setThresholdAndface() async {
    var data = await Request.getFaceRecognitionState();
    _needFace = data['status'];
    _threshold = data['score'];
    return;
  }

  void setNeedNewFace(String face) {
    _needNewFace = face;
  }

  void setTabIndex(int index) {
    _tabIndex = index;
  }

  void setDriverJson(var driverJson) {
    _driverJson = driverJson;
  }

  /// 运行方式：0010->带车入驻；0020->租车入驻
  String selectAccessionModeNum = '0010';

  /// 设置token
  void _setToken(data) {
    _token = data;
  }

  get setToken => _setToken;

  /// 个人,司机,用户信息信息
  static DriverInformationModel _driverInfo = DriverInformationModel(
      idCardUploadState: false,
      drivingUploadState: false,
      qualificationCertificateState: false,
      driverMpdify: '0000', // 驾驶证与身份证信息是否可以修改状态, 0000可修改 0010不可修改
      vehicleMpdify: '0000', // 车主车辆信息是否可以修改状态, 0000可修改 0010不可修改
      qualificationMpdify: '0000', // 资格证信息是否可以修改, 0000可修改 0010不可修改
      authenticationStatus: '0000',
      workingStatus: '0030',
      carImageList: [],
      brandModels: '');
  get driverInfo => _driverInfo;

  /// 车辆信息
  VehicleInformationModel _vehicleInformationInfo;
  get vehicleInformationInfo => _vehicleInformationInfo;

  void cleanDriverInfo() {
    _driverInfo = DriverInformationModel(
        idCardUploadState: false,
        drivingUploadState: false,
        qualificationCertificateState: false,
        driverMpdify: '0000', // 驾驶证与身份证信息是否可以修改状态, 0000可修改 0010不可修改
        vehicleMpdify: '0000', // 车主车辆信息是否可以修改状态, 0000可修改 0010不可修改
        qualificationMpdify: '0000', // 资格证信息是否可以修改, 0000可修改 0010不可修改
        authenticationStatus: '0000',
        workingStatus: '0030',
        carImageList: [],
        brandModels: '');
  }

  /// 设置司机信息
  void setDriverInformation({
    String userId, // ID
    String driverName,
    String headimgurl,
    String driverPhone,
    String authenticationStatus,
    String idNo,
    String workingStatus,
    String frontIdCard,
    String backIdCard,
    String driverType,
    String frontDriverLicense,
    String backDriverLicense,
    String frontDrivingLicense,
    String backDrivingLicense,
    String dateTimeCertification,
    String employment,
    String operate,
    String termValidity,
    bool idCardUploadState,
    bool qualificationCertificateState,
    String vehicleNumberPlate,
    String brandModels,
    bool drivingUploadState,
    String vehicleColor,
    String vin,
    String engineid,
    String distance,
    String certifyDate,
    String carAddress,
    List<String> carImageList,
    String driverMpdify,
    String vehicleMpdify,
    String qualificationMpdify,
  }) {
    if (userId != null) _driverInfo.userId = userId; // ID
    if (driverName != null) _driverInfo.driverName = driverName; // 司机姓名
    if (headimgurl != null) _driverInfo.headimgurl = headimgurl; // 头像
    if (driverPhone != null) _driverInfo.driverPhone = driverPhone; // 手机号
    if (authenticationStatus != null) {
      // if(authenticationStatus == '0000') {
      //   print('99999999999999');
      //   eventBus.fire(new DriverCertification(true));
      // }
      _driverInfo.authenticationStatus = authenticationStatus; // 车主认证状态
    }
    if (idNo != null) _driverInfo.idNo = idNo; // 身份证号
    if (workingStatus != null)
      _driverInfo.workingStatus = workingStatus; // 接单状态(出车状态)
    if (frontIdCard != null) _driverInfo.frontIdCard = frontIdCard; // 身份证正面
    if (backIdCard != null) _driverInfo.backIdCard = backIdCard; // 身份证背面
    if (driverType != null) _driverInfo.driverType = driverType; // 驾驶证类型
    if (frontDriverLicense != null)
      _driverInfo.frontDriverLicense = frontDriverLicense; // 驾驶证正面
    if (backDriverLicense != null)
      _driverInfo.backDriverLicense = backDriverLicense; // 驾驶证背面
    if (frontDrivingLicense != null)
      _driverInfo.frontDrivingLicense = frontDrivingLicense; // 行驶证正面
    if (backDrivingLicense != null)
      _driverInfo.backDrivingLicense = backDrivingLicense; // 行驶证背面
    if (dateTimeCertification != null)
      _driverInfo.dateTimeCertification = dateTimeCertification; // 驾驶证初领证日期
    if (termValidity != null) _driverInfo.termValidity = termValidity; // 驾驶证有效期
    if (idCardUploadState != null)
      _driverInfo.idCardUploadState = idCardUploadState; // 身份证与驾驶证上传状态
    if (drivingUploadState != null)
      _driverInfo.drivingUploadState = drivingUploadState; // 行驶证上传状态
    if (employment != null) _driverInfo.employment = employment; // 网约车驾驶证照片
    if (operate != null) _driverInfo.operate = operate; // 车辆运行证
    if (qualificationCertificateState != null)
      _driverInfo.qualificationCertificateState =
          qualificationCertificateState; // 网约车驾驶证与车辆运行证上传状态
    if (vehicleNumberPlate != null)
      _driverInfo.vehicleNumberPlate = vehicleNumberPlate; // 车辆牌号
    if (brandModels != null) _driverInfo.brandModels = brandModels; // 汽车品牌
    if (vehicleColor != null) _driverInfo.vehicleColor = vehicleColor; // 车牌信息
    if (vin != null) _driverInfo.vin = vin; // 车辆识别码
    if (engineid != null) _driverInfo.engineid = engineid; // 发动机号
    if (distance != null) _driverInfo.distance = distance; // 行驶里程
    if (certifyDate != null) _driverInfo.certifyDate = certifyDate; // 上牌时间
    if (carAddress != null) _driverInfo.carAddress = carAddress; // 车辆城市
    if (carImageList != null) _driverInfo.carImageList = carImageList; // 车辆照片

    if (driverMpdify != null) {
      /// 判断是否是车主
      if (_driverInfo.authenticationStatus == '0000')
        _driverInfo.driverMpdify = driverMpdify; // 驾驶证与身份证信息是否可以修改状态
    }

    if (vehicleMpdify != null) {
      /// 判断是否是车主
      if (_driverInfo.authenticationStatus == '0000')
        _driverInfo.vehicleMpdify = vehicleMpdify; // 车主车辆信息是否可以修改状态
    }

    if (qualificationMpdify != null) {
      /// 判断是否是车主
      if (_driverInfo.authenticationStatus == '0000')
        _driverInfo.qualificationMpdify = qualificationMpdify; // 资格证信息是否可以修改状态
    }
    notifyListeners();
  }

  /// 修改车辆照片
  void setVehiclePhotos({
    String carImage, // ID
  }) {
    _driverInfo.carImageList.add(carImage);
    notifyListeners();
  }

  // 删除单个选择的车辆照片
  void delCarImage(data) async {
    _driverInfo.carImageList.remove(data);
    notifyListeners();
  }

  // 设置车主加入方式
  setAccessionMode(data) {
    if (data == '自己带车入驻') {
      selectAccessionModeNum = '0010';
    } else {
      selectAccessionModeNum = '0020';
    }
    notifyListeners();
  }

  /// 修改头像
  setHeadimgurl() async {
    try {
      var image = await ImagePicker.pickImage(source: ImageSource.gallery);
      if (image == null) return '';

      //普通上传
      Request.uploadHeadImg(image.path).then((res) {
        if (res == '') return;
        _driverInfo.headimgurl = res;
        notifyListeners();
      });
    } catch (e) {}
  }
}
